import os
import sys
import json
import time
import ctypes



START_EXE_PATH = os.path.abspath(sys.argv[0])
VERSION = "1.1.0"

# 前景色

FOREGROUND_BLACK = 0x00     # 黑色
FOREGROUND_BLUE = 0x01      # 蓝色
FOREGROUND_GREEN = 0x02     # 绿色
FOREGROUND_CYAN = 0x03      # 蓝绿色
FOREGROUND_RED = 0x04       # 红色
FOREGROUND_MAGENTA = 0x05   # 品红色
FOREGROUND_YELLOW = 0x06    # 黄色
FOREGROUND_WHITE = 0x07     # 白色
FOREGROUND_GRAY = 0x08      # 灰色
FOREGROUND_INTENSITY = 0x08 # 加强前景色，通常与其他颜色组合使用
#背景色
BACKGROUND_BLACK = 0x00     # 黑色背景
BACKGROUND_BLUE = 0x10      # 蓝色背景
BACKGROUND_GREEN = 0x20     # 绿色背景
BACKGROUND_CYAN = 0x30      # 蓝绿色背景
BACKGROUND_RED = 0x40       # 红色背景
BACKGROUND_MAGENTA = 0x50   # 品红色背景
BACKGROUND_YELLOW = 0x60    # 黄色背景
BACKGROUND_WHITE = 0x70     # 白色背景
BACKGROUND_INTENSITY = 0x80 # 加强背景色，通常与其他颜色组合使用
HCONSOLE = ctypes.windll.kernel32.GetStdHandle(-11)



class Config(object):
    def __init__(self):
        self.json_ini = os.path.join(os.path.dirname(START_EXE_PATH), "config.json") 
        try:
            with open(self.json_ini, "r", encoding="utf-8") as f:
                self._dict = json.load(f)
        except Exception as e:
            self._dict = {
                "ipaddr": "",		# IP地址
                "wbookid": "",		# 绑定表ID
                "wuserid": "",		# 绑定数据ID
                "statrun": False, 	# 开机自启
                "statcmd": False,	# 数据转发
                "listcmd": [],		# 挂载任务
                "bindid": 0,		# 手动绑定的行数标记
                "bind": {},			# 绑定表的详细信息
                "data": {},			# 绑定数据详细信息
            }
            with open(self.json_ini, "w", encoding="utf-8") as f:
                json.dump(self._dict, f, ensure_ascii=False, indent=4)
    
    def setIni(self, key, value):
        self._dict[key] = value
        with open(self.json_ini, "w", encoding="utf-8") as f:
            json.dump(self._dict, f, ensure_ascii=False, indent=4)




def error_log(log):
    with open(os.path.join(os.path.dirname(START_EXE_PATH), "error.log"), "a", encoding="utf-8") as e:
        e.write("{} : {}\n".format(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())), log))